package com.balance.anole.client.retail.dialog;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.balance.anole.business.to.ShopPaymentTO;
import com.balance.anole.client.retail.payment.CashPayment;
import com.balance.anole.client.retail.payment.Payment;
import com.balance.anole.client.retail.payment.PaymentHelper;
import com.balance.anole.common.to.CodeLookTO;
import com.balance.anole.erp.component.CodeLookComboViewer;
import com.balance.anole.erp.security.SessionManager;
import com.balance.anole.erp.util.Messages;
import com.balance.anole.erp.util.NumberUtil;
import com.balance.anole.erp.util.SWTResourceManager;
import com.balance.anole.utils.Constants;
import com.balance.anole.utils.DateUtil;

public class RetailCloseDialog extends Dialog{

	private BigDecimal needPay;

	private Text text_pay;

	private Label label_changeString;

	private String totalPaidValue;

	private String totalChangeValue;

	private CodeLookComboViewer comboViewer_payType;

	private Set<ShopPaymentTO> shopPaymentTOs;

	private boolean isReject;

	//default to cash payment
	private Payment currentPayment = new CashPayment(BigDecimal.ZERO, BigDecimal.ZERO);

	public Set<ShopPaymentTO> getShopPaymentTOs() {
		return shopPaymentTOs;
	}

	public void setShopPaymentTOs(Set<ShopPaymentTO> shopPaymentTOs) {
		this.shopPaymentTOs = shopPaymentTOs;
	}

	public RetailCloseDialog(Shell parentShell,String amount) {
		super(parentShell);
		BigDecimal value = new BigDecimal(amount);
		this.isReject = value.signum() < 0;
		this.needPay = value.abs();
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		if(isReject){
			newShell.setText("退款");
		}else{
			newShell.setText("收款");
		}
	}

	protected Control createDialogArea(final Composite parent) {
		Composite area = (Composite)super.createDialogArea(parent);

		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		area.setLayout(gridLayout);

		Label label_needPay = new Label(area,SWT.NONE);
		if(isReject){
			label_needPay.setText("应      退:");
		} else {
			label_needPay.setText("应      收:");
		}
		label_needPay.setFont(SWTResourceManager.getFont("", 16, SWT.NONE));

		GridData labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		label_needPay.setLayoutData(labelGd);

		final Label lable_needPayString = new Label(area,SWT.NONE);
		lable_needPayString.setText(NumberUtil.formatMoney(needPay));
		lable_needPayString.setAlignment(SWT.RIGHT);
		lable_needPayString.setFont(SWTResourceManager.getFont("", 16, SWT.BOLD));
		GridData valueGd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		valueGd.widthHint = 100;
		valueGd.heightHint = 30;
		lable_needPayString.setLayoutData(valueGd);

		Label label_payType = new Label(area,SWT.NONE);
		label_payType.setText("方      式:");
		label_payType.setFont(SWTResourceManager.getFont("", 16, SWT.NONE));
		label_payType.setLayoutData(labelGd);

		comboViewer_payType = new CodeLookComboViewer(Constants.CODE_TYPE_PAYMENT_TYPE, area, SWT.BORDER|SWT.READ_ONLY, true);
//		comboViewer_payType.getCombo().setFont(SWTResourceManager.getFont("", 16, SWT.NONE));

		comboViewer_payType.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				CodeLookTO paymentMode = (CodeLookTO) ((StructuredSelection)event.getSelection()).getFirstElement();
				currentPayment = PaymentHelper.getProperPayment(paymentMode);
				currentPayment.setNeedPay(needPay);
				String paidStr = "0.00";
				if( ! StringUtils.isEmpty(text_pay.getText())){
					paidStr = text_pay.getText();
				}
				currentPayment.setPaid(new BigDecimal(paidStr));
			}
		});

		comboViewer_payType.setLayoutData(valueGd);
		Label label_pay = new Label(area,SWT.NONE);
		if(isReject){
			label_pay.setText("实      退:");
		} else {
			label_pay.setText("实      收:");
		}
		label_pay.setFont(SWTResourceManager.getFont("", 16, SWT.NONE));
		label_pay.setLayoutData(labelGd);

		if(isReject){
			text_pay = new Text(area,SWT.BORDER|SWT.LEFT);
		} else {
			text_pay = new Text(area,SWT.BORDER|SWT.RIGHT);
		}

		text_pay.setFont(SWTResourceManager.getFont("", 16, SWT.BOLD));
		text_pay.setLayoutData(valueGd);


		text_pay.addVerifyListener(new VerifyListener() {
			public void verifyText(VerifyEvent e) {
				String money = text_pay.getText().substring(0, e.start) + e.text + text_pay.getText().substring(e.end);
				if(StringUtils.isEmpty(money)){
					e.doit = true;
				}else if (NumberUtil.isValidMoney(money)){
					e.doit = true;
				}else{
					e.doit = false;
				}
			}
		});

		//default to cash
		comboViewer_payType.setSelection(new StructuredSelection(new CodeLookTO(Constants.CODE_TYPE_PAYMENT_TYPE, "C")));


		Label label_change = new Label(area,SWT.NONE);
		label_change.setText("找      零:");
		label_change.setFont(SWTResourceManager.getFont("", 16, SWT.NONE));
		label_change.setLayoutData(labelGd);

		label_changeString = new Label(area,SWT.NONE);
		label_changeString.setText("0.00");
		label_changeString.setAlignment(SWT.RIGHT);
		label_changeString.setFont(SWTResourceManager.getFont("", 16, SWT.BOLD));
		label_changeString.setLayoutData(valueGd);

		/*text_pay.addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent e) {

			}
			@Override
			public void keyReleased(KeyEvent e) {
				if(text_pay == null || text_pay.getText() == null || "".equals(text_pay.getText())){
					label_changeString.setText("0.00");
				}else{
					currentPayment.setNeedPay(needPay);
					currentPayment.setPaid(new BigDecimal(text_pay.getText()));

					label_changeString.setText(NumberUtil.formatMoney(currentPayment.pay()));

				}
			}
		});*/

		text_pay.addModifyListener(new ModifyListener(){

			@Override
			public void modifyText(ModifyEvent e) {
				if(text_pay == null || text_pay.getText() == null || "".equals(text_pay.getText())){
					label_changeString.setText("0.00");
				}else{
					currentPayment.setNeedPay(needPay);
					currentPayment.setPaid(new BigDecimal(text_pay.getText()));

					label_changeString.setText(NumberUtil.formatMoney(currentPayment.pay()));

				}

			}
		});

		text_pay.setText(NumberUtil.formatMoney(needPay));

		//default to money input box.
		text_pay.forceFocus();
		text_pay.selectAll();

		return area;
	}

	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, "确定",
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				"取消", false);
	}

	protected void okPressed(){

		CodeLookTO paymentMode = (CodeLookTO)((StructuredSelection)comboViewer_payType.getSelection()).getFirstElement();

		if(text_pay == null || text_pay.getText() == null || "".equals(text_pay.getText()) || currentPayment.pay().floatValue() < 0){
			MessageDialog.openError(getShell(), Messages.DIALOG_INFO_ERROR, "付款不足！");
		}else{
			if(paymentMode !=null){
				if( ! currentPayment.isValid()){
					MessageDialog.openError(getShell(), Messages.DIALOG_INFO_ERROR, currentPayment.getPaymentValidationMsg());
					return;
				}
				shopPaymentTOs = new HashSet<ShopPaymentTO>();

				ShopPaymentTO shopPaymentTO = new ShopPaymentTO();
				shopPaymentTO.setPaidValue(new BigDecimal(text_pay.getText()));
				shopPaymentTO.setPaymentMode(paymentMode);
				shopPaymentTO.setCreateBy(SessionManager.getInstance().getLoginUser().getStaffInfo());
				shopPaymentTO.setCreateDt(DateUtil.getSystemCurrentDate());

				shopPaymentTOs.add(shopPaymentTO);

				totalPaidValue = text_pay.getText();
				totalChangeValue = label_changeString.getText();

				super.okPressed();
			}else{
				MessageDialog.openError(getShell(), Messages.DIALOG_INFO_ERROR, "未选择付款方式！");
			}

		}
	}


	public String getTotalPaidValue() {
		return totalPaidValue;
	}

	public void setTotalPaidValue(String totalPaidValue) {
		this.totalPaidValue = totalPaidValue;
	}

	public String getTotalChangeValue() {
		return totalChangeValue;
	}

	public void setTotalChangeValue(String totalChangeValue) {
		this.totalChangeValue = totalChangeValue;
	}
}
